{include file="../application/mobile/view/header.html" /}
</head>
<body>
<div class="bui-page">
    <header class="bui-bar">
        <div class="bui-bar-left">
            <a class="bui-btn" onclick="bui.back();"><i class="icon-back"></i></a>
        </div>
        <div class="bui-bar-main">{$question.question_name}</div>
        <div style="display: none;" id="questions_id">{$question.id}</div>
        <div class="bui-bar-right">
            <!-- 右边按钮位置 -->
        </div>
    </header>
    <main>
        <!-- 正文内容 -->
        {if condition="$ifdone eq 'done'"}
        <div class="section-title">分数：{$score}</div>
        <hr/>
         {volist name="question_list" id="question" key="k"}
        <div class="section-title">Q{$question.question_id}:{$question.question}</div>
        {switch name="question.type"}
        {case value="1"}
        <ul class="bui-list">
        	{volist name="$question.answers" id="qa" key="i"}
            <li class="bui-btn bui-box">
                <input id="rodio-{$k}-{$i}" type="radio" class="bui-radio" name="rodio-{$k}" value="{$i}" {if condition="$question.true_answer eq $i"} checked="checked" {/if}>
                <div class="span1">
                    <label for="rodio-{$k}-{$i}" {if condition="$question.iftrue neq '1' && $question.user_answer eq $i "} style="color:#E74C3C;" {/if}>{$qa}</label>
                </div>
            </li>
            {/volist}
        </ul>
        {/case}

        {case value="2"}
        <ul class="bui-list">
        	{volist name="$question.answers" id="qa" key="i"}
            <li class="bui-btn bui-box">
                <input id="checkbox-{$k}-{$i}" type="checkbox" class="bui-checkbox" name="checkbox-{$k}" value="" text="" {in name="i" value="$question.true_answer" } checked="checked" {/in}>
                <div class="span1">
                    <label for="checkbox-{$k}-{$i}" {if condition="$question.iftrue neq '1'"}{in name="i" value="$question.user_answer"} style="color:#E74C3C;" {/in} {/if} >{$qa}</label>
                </div>
            </li>
            {/volist}
        </ul>
        {/case}

        {case value="3"}
        <div class="bui-input">
            <textarea placeholder="" id="" cols="30" rows="8" disabled="disabled">{$question.user_answer}</textarea>
        </div>
        <div style="color: #E74C3C;">参考答案：{$question.true_answer}</div>
        {/case}
        {/switch}
        {/volist}



        {else/}
        <div id="question_content">
        {volist name="question_list" id="question" key="k"}
        <div class="_item" data-question_id="{$question.question_id}" data-type="{$question.type}">
        <div class="section-title">Q{$question.question_id}:{$question.question}</div>
        {switch name="question.type"}
        {case value="1"}
        <ul class="bui-list">
        	{volist name="$question.answers" id="qa" key="i"}
            <li class="bui-btn bui-box">
                <input id="rodio-{$k}-{$i}" type="radio" class="bui-radio" name="rodio-{$k}" value="{$i}">
                <div class="span1">
                    <label for="rodio-{$k}-{$i}">{$qa}</label>
                </div>
            </li>
            {/volist}
        </ul>
        {/case}

        {case value="2"}
        <ul class="bui-list">
        	{volist name="$question.answers" id="qa" key="i"}
            <li class="bui-btn bui-box">
                <input id="checkbox-{$k}-{$i}" type="checkbox" class="bui-checkbox" name="checkbox-{$k}" value="" text="">
                <div class="span1">
                    <label for="checkbox-{$k}-{$i}">{$qa}</label>
                </div>
            </li>
            {/volist}
        </ul>
        {/case}

        {case value="3"}
        <div class="bui-input">
            <textarea placeholder="请输入您的答案" id="" cols="30" rows="8"></textarea>
        </div>
        {/case}
        {/switch}
        <div>
        {/volist}
        </div>
        {/if}
    </main>
    <footer>
    	{if condition="$role eq 'admin' "}
        <div class="bui-btn disabled">管理员不参与答题!</div>
 		{/if}
 		{if condition="$ifdone eq 'never' &&  $role neq 'admin'"}
        <div class="bui-btn primary" id="submit">提交</div>
        {/if}
    </footer>
</div>





{include file="../application/mobile/view/footer.html" /}
<script>
        bui.ready(function () {

 });


 bui.btn("#submit").submit(function(loading){
 	var questions_id = $('#questions_id').html();
  	var user_answer = new Array;
 	 $('#question_content').find('._item').each(function(){
    var _this = $(this);
    var sub_user_answer = {};
    sub_user_answer['question_id'] = _this.attr('data-question_id');
    sub_user_answer['type'] = _this.attr('data-type');
    switch (sub_user_answer['type']){
      case '1':
      var radio = _this.find('input').val();
      
      sub_user_answer['answer'] = radio;
      break;
      case '2':
        var checkbox = _this.find('input');
        var check_checkbox = new Array;
        for (var i = 0; i < checkbox.length; i++) {
          if (checkbox[i].checked) {
            a = i+1;
            check_checkbox.push(a);
          }
        }
        sub_user_answer['answer'] = check_checkbox.join('&');
      break;
      case '3':
      sub_user_answer['answer'] = _this.find('textarea').val();
      break;
    }
    sub_user_answer['iftrue'] = false;
    user_answer.push(sub_user_answer);

 });

 	bui.ajax({
 	    url: "save_user_answer",
 	    data:{"questions_id":questions_id,"user_answer":user_answer },//接口请求的参数
 	    // 可选参数
 	    method: "POST"
 	}).then(function(result){
 	    if (result.err == '0') {
 	    	bui.hint({ appendTo:"main", content:"<i class='icon-infofill'></i>"+result.content, position:"top" , skin:'success', showClose:true, autoClose: false});
 	    }else{
 	    	bui.hint({ appendTo:"main", content:"<i class='icon-infofill'></i>"+result.content, position:"top" , skin:'danger', showClose:true, autoClose: false});

 	    }
 	},function(result,status){
 	    // 失败 
 	});
 	loading.stop();
 });

 // bui.btn({id:".bui-page",handle:".bui-btn,a"}).load();

</script>
</body>
</html>